/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.bean.client;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import model.client.City;
import model.client.Country;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class TestBean {//Table name constant

    private final String TABLE_ACCOUNTS = "Accounts";
    private final String TABLE_ACCOUNTS_DETAILS = "Account_Details";
    private final String TABLE_COUNTRY = "Countries";
    private final String TABLE_CITIES = "Cities";
    private final String TABLE_PAYMENT_TYPE = "Payment_Types";
    //Field name constant
    private final String CITY_ID = "City_Id";
    private final String CITY_NAME = "City_Name";
    private final String COUNTRY_ID = "Country_Id";
    private final String COUNTRY_STATUS = "Country_Status";
    private final String COUNTRY_NAME = "Country_Name";

    Connection conn;
    Country country;
    City city;

    List<Country> listCountries;
    List<City> listCities;

    public List<Country> getListCountries() {
        return listCountries;
    }

    public List<City> getListCities() {
        return listCities;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    /**
     * Creates a new instance of TestBean
     */
    public TestBean() throws SQLException, ClassNotFoundException {
        conn = JDBCUtil.MyConn.getConnection();
    }

    @PostConstruct
    public void listAllCountries() throws SQLException {
        PreparedStatement pst = conn.prepareStatement("SELECT * FROM " + TABLE_COUNTRY);
        ResultSet rs = pst.executeQuery();
        
        listCountries = new ArrayList<Country>();
        while (rs.next()) {
            listCountries.add(new Country(rs.getInt(COUNTRY_ID), rs.getBoolean(COUNTRY_STATUS), rs.getString(COUNTRY_NAME)));
        }
    }

    public void listAllCitiesByCountryId() throws SQLException {
        PreparedStatement pst = conn.prepareStatement("SELECT * FROM " + TABLE_CITIES
                + " WHERE " + COUNTRY_ID + " = " + country.getCountryId());

        ResultSet rs = pst.executeQuery();
        if (listCities == null) {
            listCities = new ArrayList<City>();
        }
        while (rs.next()) {
            listCities.add(new City(rs.getInt(CITY_ID), rs.getInt(COUNTRY_ID), rs.getString(CITY_NAME)));
        }
    }
    
    public void testRegister(){
        String str = String.valueOf(country.getCountryId());
        String str2 = String.valueOf(city.getCityId());
    }
}
